<template>
	<view class="container">
		<!-- 顶部商品信息 -->
		<view class="goods">
			<view class="goods-image">
				<image :src="goods.post_url"></image>
			</view>
			<view class="goods-name"><text>{{goods.title}}</text></view>
		</view>
		<view class="content">
			<!-- 用户评价 -->
			<view class="comment" >
				<view class="comment-info" v-for="(item, index) in list" :key="item.id">
					<view class="comment-avatar">
						<image :src="item.user.avatar_url"></image>
					</view>
					<view class="comment-right">
						<view class="comment-nickname">
							<text>{{item.user.nickname}}</text>
							<text>{{item.create_time}}</text>
						</view>
						<view class="comment-star">
							<u-rate :count="5" v-model="item.star" inactive-color="#DCDEE0" active-color="#FBA808"
								:disabled="true" size="28"></u-rate>
						</view>
						<view class="comment-content">
							{{item.content}}
						</view>
						<view class="comment-image" v-if="item.images.length > 0">
							<view class="comment-image-item" v-for="(image, index) in item.images" :key="index">
								<image :src="image" :lazy-load="true" @click="viewImage(item.images, index)"></image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 底部栏 -->
		<view class="bottombar">
			<view class="bottombar-content">
				<view class="bottombar-content-item">
					<image src="/static/icon/love.png" @click="toCollect()" v-if="!collectState"></image>
					<image src="/static/icon/nolove.png" @click="toCollect()" v-else></image>
					<text>收藏</text>
				</view>
				<view class="bottombar-content-item">
					<image src="/static/icon/kefu2.png"></image>
					<text>客服</text>
				</view>
			</view>
			<view class="bottombar-btn">
				<button @click="popupAddCart()">加入购物车</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
        goods: {},
				list: [
				],
				// 收藏状态
				collectState: false,
        resCount: 0,
        count: 1,
			}
		},
    onLoad({ productId }) {
      this.$api.getProductDetail(productId).then(res =>{
        this.goods = res
        this.list = res.comments.map(item => {
          return {
            ...item,
            images: item.image_urls ? item.image_urls.split(',') : []
          }
        })
      })
      this.getCollect(productId)

      this.$api.getCartList({
        product_id: productId
      }).then(res => {
        if (res.rows.length) {
          this.resCount = res.rows[0].count
        }
      })
    },
		methods: {
      getCollect(id) {
        this.$api.getCollectionList({
          user_id: this.$store.state.userInfo.id,
          product_id: id
        }).then(res => {
          if (res.rows.length) {
            this.collectState = true
          } else {
            this.collectState = false
          }
        })
      },
      toCollect() {
        uni.showLoading()
        this.$api.updateCollect({
          user_id: this.$store.state.userInfo.id,
          product_id: this.goods.id
        }).then(res => {
          this.collectState = !this.collectState
          this.$utils.toastTip("收藏成功");
        })
      },
			// 点击放大图片
      viewImage(image, index) {
        uni.previewImage({
          urls: image,
          current: image[index]
        })
      },
			// 收藏/取消收藏
			collect() {
				this.$u.throttle(this.toCollect, 2000)
			},

      // 弹出层加入购物车
      popupAddCart() {
        uni.showLoading()
        this.$api.addCart({
          user_id: this.$store.state.userInfo.id,
          count: 1,
          product_id: this.goods.id
        }).then(res => {
          this.$utils.toastTip("成功加入购物车");
        })
      },
		}
	}
</script>

<style>
	page {
		background: #F8F9FA;
	}
</style>

<style lang="scss" scoped>
	.goods {
		display: flex;
		align-items: center;
		background: #FFFFFF;
		padding: 10rpx 30rpx;

		.goods-image {
			image {
				width: 100rpx;
				height: 100rpx;
			}
		}

		.goods-name {
			margin-left: 10rpx;

			text {
				font-size: 26rpx;
				font-weight: 600;
				color: #333333;
			}
		}
	}

	.content {
		margin: 20rpx;
		padding-bottom: 120rpx;

		.comment {
			margin-top: 20rpx;

			.comment-avatar {
				image {
					width: 70rpx;
					height: 70rpx;
					border-radius: 50%;
				}
			}

			.comment-info {
				display: flex;
				background: #FFFFFF;
				padding: 30rpx 20rpx;
				border-radius: 20rpx;
				margin-bottom: 20rpx;

				.comment-right {
					width: 100%;
					padding: 0rpx 20rpx;

					.comment-nickname {
						display: flex;
						align-items: center;
						justify-content: space-between;

						text:nth-child(1) {
							font-size: 24rpx;
							font-weight: 600;
							color: #333333;
						}

						text:nth-child(2) {
							font-size: 22rpx;
							color: #999999;
						}
					}

					.comment-content {
						margin-top: 20rpx;
						color: #666666;
						font-size: 24rpx;
					}

					.comment-image {
						display: flex;
						align-items: center;
						flex-wrap: wrap;

						.comment-image-item {
							margin-right: 20rpx;
							margin-top: 20rpx;
						}

						image {
							width: 112rpx;
							height: 112rpx;
						}
					}
				}

			}
		}
	}

	.bottombar {
		position: fixed;
		bottom: var(--window-bottom, 0);
		width: 100%;
		background: #FFFFFF;
		display: flex;
		align-items: center;

		.bottombar-content {
			display: flex;
			align-items: center;
			padding: 20rpx;

			image {
				width: 40rpx;
				height: 38rpx;
			}

			.bottombar-content-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 60rpx;
				min-width: 60rpx;

				text {
					margin-top: 2rpx;
				}
			}
		}

		.bottombar-btn {
			display: flex;
			align-items: center;
			width: 100%;
			padding-right: 20rpx;

			button {
				flex: 1;
				border-radius: 36rpx;
				background: #40AE36;
				color: #FFFFFF;
				font-size: 26rpx;
			}
		}
	}
</style>
